{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8ceb7936050f2dd8",
   "metadata": {},
   "source": [
    "一.DataFrame的基本概念"
   ]
  },
  {
   "cell_type": "raw",
   "id": "83ab25ef2fc45bb2",
   "metadata": {},
   "source": [
    "二维表格：DataFrame 由行和列组成，每一列可以是不同的数据类型（如整数、浮点数、字符串等）。\n",
    "标签索引：行和列都有标签（索引），行索引称为 index，列索引称为 columns。\n",
    "灵活性和高效性：DataFrame 提供了丰富的数据操作功能，如数据筛选、合并、分组、聚合等。"
   ]
  },
  {
   "cell_type": "raw",
   "id": "c977590ea4a90e5f",
   "metadata": {},
   "source": [
    "1.创建 DataFrame"
   ]
  },
  {
   "cell_type": "raw",
   "id": "b25a2fe9b1157363",
   "metadata": {},
   "source": [
    "可以通过多种方式创建 DataFrame，以下是几种常见方法："
   ]
  },
  {
   "cell_type": "raw",
   "id": "f4407f9c9fd15992",
   "metadata": {},
   "source": [
    "a.从子典创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f5c3511f6ea523e7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-18T10:11:14.967642Z",
     "start_time": "2025-02-18T10:11:14.083855Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      Name  Age         City\n",
      "0    Alice   25     New York\n",
      "1      Bob   30  Los Angeles\n",
      "2  Charlie   35      Chicago\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "data = {\n",
    "    'Name': ['Alice', 'Bob', 'Charlie'],\n",
    "    'Age': [25, 30, 35],\n",
    "    'City': ['New York', 'Los Angeles', 'Chicago']\n",
    "}\n",
    "\n",
    "df = pd.DataFrame(data)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "22e1707a36d4b007",
   "metadata": {},
   "source": [
    "b.从列表创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bf526095fa0022ed",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-18T10:11:17.256002Z",
     "start_time": "2025-02-18T10:11:17.249234Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      Name  Age         City\n",
      "0    Alice   25     New York\n",
      "1      Bob   30  Los Angeles\n",
      "2  Charlie   35      Chicago\n"
     ]
    }
   ],
   "source": [
    "data = [\n",
    "    ['Alice', 25, 'New York'],\n",
    "    ['Bob', 30, 'Los Angeles'],\n",
    "    ['Charlie', 35, 'Chicago']\n",
    "]\n",
    "\n",
    "df = pd.DataFrame(data, columns=['Name', 'Age', 'City'])\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "a3809cc199d8f0e1",
   "metadata": {},
   "source": [
    "c.从外部文件加载"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "31da4c1d5de040e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 从 CSV 文件加载\n",
    "df = pd.read_csv('data.csv')\n",
    "\n",
    "# 从 Excel 文件加载\n",
    "df = pd.read_excel('data.xlsx')"
   ]
  },
  {
   "cell_type": "raw",
   "id": "45dee19ed145347f",
   "metadata": {},
   "source": [
    "2.DataFrame 的基本操作"
   ]
  },
  {
   "cell_type": "raw",
   "id": "fbfcfd4055c3674d",
   "metadata": {},
   "source": [
    "(1) 查看数据\n",
    "查看前几行：df.head(n)（默认显示前 5 行）\n",
    "\n",
    "查看后几行：df.tail(n)\n",
    "\n",
    "查看基本信息：df.info()\n",
    "\n",
    "查看统计信息：df.describe()\n",
    "\n",
    "(2) 选择数据\n",
    "选择列：df['列名'] 或 df.列名\n",
    "\n",
    "选择行：df.loc[行索引] 或 df.iloc[行号]\n",
    "\n",
    "条件筛选：df[df['Age'] > 30]\n",
    "\n",
    "(3) 添加/删除数据\n",
    "添加列：df['新列名'] = 值\n",
    "\n",
    "删除列：df.drop('列名', axis=1, inplace=True)\n",
    "\n",
    "删除行：df.drop(行索引, axis=0, inplace=True)\n",
    "\n",
    "(4) 修改数据\n",
    "修改某个值：df.at[行索引, '列名'] = 新值\n",
    "\n",
    "修改整列：df['列名'] = 新值\n",
    "\n",
    "(5) 排序\n",
    "按列排序：df.sort_values('列名', ascending=True/False)\n",
    "\n",
    "(6) 分组和聚合\n",
    "分组：df.groupby('列名')\n",
    "\n",
    "聚合：df.groupby('列名').agg({'列名': 'mean'})\n",
    "\n",
    "4. DataFrame 的常用属性\n",
    "df.shape：返回 DataFrame 的行数和列数。\n",
    "\n",
    "df.columns：返回列名列表。\n",
    "\n",
    "df.index：返回行索引。\n",
    "\n",
    "df.dtypes：返回每列的数据类型。\n",
    "\n",
    "df.values：将 DataFrame 转换为 NumPy 数组。\n",
    "\n",
    "5. DataFrame 的常用方法\n",
    "数据清洗：\n",
    "\n",
    "处理缺失值：df.dropna()（删除缺失值）、df.fillna(值)（填充缺失值）。\n",
    "\n",
    "去重：df.drop_duplicates()。\n",
    "\n",
    "数据合并：\n",
    "\n",
    "连接：pd.concat([df1, df2])。\n",
    "\n",
    "合并：pd.merge(df1, df2, on='键列')。\n",
    "\n",
    "数据透视表：\n",
    "\n",
    "df.pivot_table(values='值列', index='行索引列', columns='列索引列', aggfunc='mean')。"
   ]
  },
  {
   "cell_type": "raw",
   "id": "2ec1bea079ce7ada",
   "metadata": {},
   "source": [
    "3.示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "66505d1d12937492",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-18T10:15:12.609425Z",
     "start_time": "2025-02-18T10:15:12.586820Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数据：\n",
      "      Name  Age         City  Salary\n",
      "0    Alice   25     New York   50000\n",
      "1      Bob   30  Los Angeles   60000\n",
      "2  Charlie   35      Chicago   70000\n",
      "3    David   40      Houston   80000\n",
      "\n",
      "筛选后的数据：\n",
      "      Name  Age     City  Salary\n",
      "2  Charlie   35  Chicago   70000\n",
      "3    David   40  Houston   80000\n",
      "\n",
      "分组聚合结果：\n",
      "              Salary\n",
      "City                \n",
      "Chicago      70000.0\n",
      "Houston      80000.0\n",
      "Los Angeles  60000.0\n",
      "New York     50000.0\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 创建 DataFrame\n",
    "data = {\n",
    "    'Name': ['Alice', 'Bob', 'Charlie', 'David'],\n",
    "    'Age': [25, 30, 35, 40],\n",
    "    'City': ['New York', 'Los Angeles', 'Chicago', 'Houston']\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "\n",
    "# 添加新列\n",
    "df['Salary'] = [50000, 60000, 70000, 80000]\n",
    "\n",
    "# 筛选数据\n",
    "filtered_df = df[df['Age'] > 30]\n",
    "\n",
    "# 分组聚合\n",
    "grouped_df = df.groupby('City').agg({'Salary': 'mean'})\n",
    "\n",
    "print(\"原始数据：\")\n",
    "print(df)\n",
    "print(\"\\n筛选后的数据：\")\n",
    "print(filtered_df)\n",
    "print(\"\\n分组聚合结果：\")\n",
    "print(grouped_df)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "819e0b4d76b32de6",
   "metadata": {},
   "source": [
    "# 二、sklearn中MinMaxScaler\n",
    "`MinMaxScaler` 是 `scikit-learn` 库中的一个数据预处理工具，用于将特征缩放到一个指定的范围，通常是 [0, 1]。这种缩放方法称为**最小-最大缩放**或**归一化**。\n",
    "\n",
    "### 工作原理\n",
    "`MinMaxScaler` 通过对每个特征进行线性变换，将数据缩放到给定的范围。具体来说，对于每个特征，它会执行以下操作：\n",
    "\n",
    "$ X_{\\text{scaled}} = \\frac{X - X_{\\text{min}}}{X_{\\text{max}} - X_{\\text{min}}} $\n",
    "\n",
    "其中：\n",
    "- $ X $ 是原始数据。\n",
    "- $ X_{\\text{min}} $ 是该特征的最小值。\n",
    "- $ X_{\\text{max}} $ 是该特征的最大值。\n",
    "- $ X_{\\text{scaled}} $ 是缩放后的数据。\n",
    "\n",
    "默认情况下，`MinMaxScaler` 将数据缩放到 [0, 1] 范围内，但你也可以通过设置 `feature_range` 参数来指定其他范围。\n",
    "\n",
    "### 主要参数\n",
    "- **feature_range**: 一个元组，表示缩放后的范围，默认为 (0, 1)。\n",
    "- **copy**: 是否复制数据，默认为 `True`。如果为 `False`，则直接在原数据上进行操作。\n",
    "\n",
    "### 主要方法\n",
    "- **fit(X)**: 计算数据 `X` 的最小值和最大值，以便后续的缩放操作。\n",
    "- **transform(X)**: 根据 `fit` 方法计算的最小值和最大值，对数据 `X` 进行缩放。\n",
    "- **fit_transform(X)**: 先调用 `fit` 方法，然后调用 `transform` 方法，一步完成拟合和转换。\n",
    "- **inverse_transform(X)**: 将缩放后的数据转换回原始数据。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0683eda7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数据:\n",
      " [[1 2]\n",
      " [2 3]\n",
      " [3 4]\n",
      " [4 5]]\n",
      "缩放后的数据:\n",
      " [[0.         0.        ]\n",
      " [0.33333333 0.33333333]\n",
      " [0.66666667 0.66666667]\n",
      " [1.         1.        ]]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "### 示例代码\n",
    "\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "import numpy as np\n",
    "\n",
    "# 示例数据\n",
    "data = np.array([[1, 2], [2, 3], [3, 4], [4, 5]])\n",
    "\n",
    "# 创建 MinMaxScaler 对象\n",
    "scaler = MinMaxScaler(feature_range=(0, 1))\n",
    "\n",
    "# 拟合数据并转换\n",
    "scaled_data = scaler.fit_transform(data)\n",
    "\n",
    "print(\"原始数据:\\n\", data)\n",
    "print(\"缩放后的数据:\\n\", scaled_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "676a09515d0f776f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d6772e33e635d55",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "562c149a566bd461",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "935e59f366576d99",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "844ae00f34b66cdf",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "yolov8",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
